package com.paul.ddyw.service.impl;

import com.paul.ddyw.mapper.OrderMapper;
import com.paul.ddyw.model.Order;
import com.paul.ddyw.service.OrderService;
import com.paul.sys.tools.CommPageBean;
import com.paul.sys.tools.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by yangjianbo on 2016/2/16.
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public int placeOrder(Order order) {
        return orderMapper.insert(order);
    }

    @Override
    public Boolean cancelOrder(String uuid) {
        Order order=orderMapper.searchByPriKey(uuid);
        Boolean flag=false;
        if(order!=null){
            order.setStatus("-1");
            int result=orderMapper.update(order);
            if (result==1){
                flag= true;
            }
        }
        return flag;
    }

    @Override
    public Boolean confirmOrder(String uuid) {
        Order order=orderMapper.searchByPriKey(uuid);
        Boolean flag=false;
        if(order!=null&&"1".equals(order.getStatus())){
            order.setStatus("2");
            int result=orderMapper.update(order);
            if (result==1){
                flag= true;
            }
        }
        return flag;
    }

    @Override
    public Boolean codeOrder(String uuid) {
        Order order=orderMapper.searchByPriKey(uuid);
        Boolean flag=false;
        if(order!=null&&"2".equals(order.getStatus())){
            order.setStatus("3");
            int result=orderMapper.update(order);
            if (result==1){
                flag= true;
            }
        }
        return flag;
    }

    @Override
    public Boolean reciveOrder(String uuid) {
        Order order=orderMapper.searchByPriKey(uuid);
        Boolean flag=false;
        if(order!=null&&"3".equals(order.getStatus())){
            order.setStatus("4");
            int result=orderMapper.update(order);
            if (result==1){
                flag= true;
            }
        }
        return flag;
    }

    @Override
    public Boolean payOrder(String uuid) {
        Order order=orderMapper.searchByPriKey(uuid);
        Boolean flag=false;
        if(order!=null&&"4".equals(order.getStatus())){
            order.setStatus("5");
            int result=orderMapper.update(order);
            if (result==1){
                flag= true;
            }
        }
        return flag;
    }

    @Override
    public Boolean complateOrder(String uuid) {
        Order order=orderMapper.searchByPriKey(uuid);
        Boolean flag=false;
        if(order!=null&&"5".equals(order.getStatus())){
            order.setStatus("0");
            int result=orderMapper.update(order);
            if (result==1){
                flag= true;
            }
        }
        return flag;    }

    @Override
    public int update(Order order) {
        return orderMapper.update(order);
    }

    @Override
    public Order selectByPrimaryKey(String uuid) {
        return orderMapper.searchByPriKey(uuid);
    }

    @Override
    public PageData<Order> listPage(Map param) {
        PageData<Order> pageData = new PageData<Order>();
        CommPageBean pageBean = CommPageBean.getPageBeanByParam(param);
        List list= this.orderMapper.listByPage(param,pageBean);
        pageData.setPageBean(pageBean);
        pageData.setListData(list);
        return pageData;
    }
}
